//
//  L_RememberPwdVC.swift
//  letuofu
//
//  Created by WH-JS on 2019/12/9.
//  Copyright © 2019 WH-JS. All rights reserved.
//

import UIKit
import MBProgressHUD

class L_RememberPwdVC: L_BaseViewController {
    //验证码标识
    var phoneNum = ""
    //验证码标识
    var smsVerifyCredential = ""
    //验证码
    var vaildeCode = ""
    
    //验证码输入
    let vpwdTextField = TextField()
    //新密码
    let passwdTextField = TextField()
    //确认新密码
    let sureTextField = TextField()
    //
    let tipsLab = UILabel()

    init(phoneNum:String, smsVerifyCredential:String, vaildeCode:String) {
        self.phoneNum = phoneNum
        self.smsVerifyCredential = smsVerifyCredential
        self.vaildeCode = vaildeCode
        
        super.init()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    func setRememberPwd() {
        let params :[String:Any] = ["mobile":phoneNum,
                                    "smsVerifyCredential":smsVerifyCredential,
                                    "vaildeCode":vpwdTextField.text!,
                                    "newPassWord":passwdTextField.text!]
        MBProgressHUD.showOrIncrease()
        API.request(url: .L_RememberPwd, params: params) { (result) in
            if result.success {
                MBProgressHUD.hideOrDecrease()
                
                let popup = PopupSureBox(title: "修改成功", infoText: "", sureText: "确定")
                popup.show(vc: UIViewController.topMostController() ?? self)
                popup.onSureButtonClick = {
                    //确定执行
                    self.popViewController(vCount: 3)
                }
                
            }else{
                MBProgressHUD.hideOrDecrease()
                
                let popup = PopupSureBox(title: result.errMsg ?? "修改失败", infoText: "", sureText: "确定")
                popup.show(vc: UIViewController.topMostController() ?? self)
                popup.onSureButtonClick = {
                    //确定执行
//                    self.popViewController(vCount: 3)
                }
                
            }
        }
    }
}

// MARK:- setupUI
extension L_RememberPwdVC {
    @objc override func setupUI() {
        self.view.backgroundColor = .white
        //头部图
        safeView.drawImage(imageView:headView, named: nil) { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview()
        }
        //底部图
        safeView.drawImage(imageView:footView, named: nil) { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        let titleBar = drawRoundCornerView(topOffset: 100)
        titleBar.drawLabel(text: "设置密码", fontSize: 23, fontWeight: .medium, textColor: .x333333) { (make) in
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }

        //验证码控件区域
        let vpwdView = drawImgLineView(topOffset: 28,img: nil).result
        //密码控件
        drawTextFieldInRoundCornerView(textField: vpwdTextField, placeHolder: "请输入验证码", roundCornerView: vpwdView,leftOffset: 5)
        vpwdTextField.keyboardType = .numberPad
        vpwdTextField.isSecureTextEntry = false
        
        //验证密码控件区域
        let passwdView = drawImgLineView(topOffset: 28,img: nil).result
        //密码控件
        drawTextFieldInRoundCornerView(textField: passwdTextField, placeHolder: "密码长度为8-20位，且包含英文和数字", roundCornerView: passwdView,leftOffset: 5)
        passwdTextField.isSecureTextEntry = true
        passwdTextField.addTarget(self, action: #selector(changePwd(textField:)), for: .editingChanged)
        
        //验证密码控件区域
        let sureView = drawImgLineView(topOffset: 28,img: nil)
        //密码控件
        drawTextFieldInRoundCornerView(textField: sureTextField, placeHolder: "再次输入", roundCornerView: sureView.result,leftOffset: 5)
        sureTextField.isSecureTextEntry = true
        sureTextField.addTarget(self, action: #selector(changePwd(textField:)), for: .editingChanged)
        
        passwdView.drawLabel(label: tipsLab, text: "", fontSize: 10, textColor: .red){ (make) in
            make.top.equalTo(sureTextField.snp.bottom).offset(10)
            make.left.equalTo(sureTextField.snp.left)
        }
        
        let backbtn = safeView.drawButton(imageNamed: "L_Back") { (make) in
            make.top.equalToSuperview().offset(25)
            make.left.equalTo(titleBar.snp.left)
        }
        backbtn.hitEdgInsets = .init(top: -40, left: -40, bottom: -40, right: -40)
        backbtn.addTarget(self, action: #selector(onBackCheckClicked(button:)), for: .touchUpInside)
        
        //提交登录按钮
        drawRoundCornerButton(title:"完成并确认", topOffset: 71)
            .addTarget(self, action: #selector(onSubmitButtonClick(button:)), for: .touchUpInside)
        
        initKeyboardObserver()
        fillAccessoryToolBar()
    }
}


// MARK:- 事件监听
extension L_RememberPwdVC {
    
    @objc func onSubmitButtonClick(button:UIButton){
        if vpwdTextField.text?.count ?? 0 > 0 {
            tipsLab.text = "请输入验证码"
        }
        
        if passwdTextField.text?.validatePassword() ?? false,passwdTextField.text == sureTextField.text{
            setRememberPwd()
            tipsLab.isHidden = true
        }
        else if passwdTextField.text != sureTextField.text {
            tipsLab.text = "两次密码不一致，请重新输入"
        }
        else {
            tipsLab.text = "密码不规范，请重新输入"
        }
    }
    
    @objc func changePwd(textField: UITextField) {
        tipsLab.text = ""
    }
    
    @objc func onBackCheckClicked(button:UIButton){
        popViewController(vCount: 3)
    }
}
